

TOP_PRECISION = 0
LEFT_PRECISION = 0

def setGlobalValue(top,left):
    global TOP_PRECISION
    TOP_PRECISION = top
    global LEFT_PRECISION
    LEFT_PRECISION = left
# 计算纵向排版数据
def computedCol(result,index,dis):
    res = {
        "jmp":1
    }
    cur = result[index]
    print(TOP_PRECISION)
    if dis <= TOP_PRECISION:
        subTitle = computedSub(result,index + 1,[])
        return {
            "type":"title",
            "key":cur['words'],
            "subTitle":subTitle,
            "jmp":len(subTitle)
        }
    else:
        # 单纯的输入框
        if "请输入" in cur['words']:
            return{
                "type": "input",
                "jmp": 0,
                "prop": "",
                "placeholder": cur['words'],
                "property":{
                    "type":"text"
                },
            }

        try:
            nextOne = result[index+1]
            # 带有title的输入框
            if "请输入" in nextOne['words']:
                return {
                    "type": "input",
                    "key": cur['words'],
                    "jmp": 1,
                    "prop":"",
                    "placeholder":nextOne['words'],
                }
            # 带有title的选择框
            if "请选择" in nextOne['words']:
                nextTow = False
                try:
                    nextTow = result[index + 2]
                except:
                    pass
                arrow = handleSelectArrow(nextOne,nextTow)
                res = {
                    "type":"select",
                    "key":cur['words'],
                    "prop":"",
                    "placeholder":arrow['words'],
                    "options":[],
                    "jmp":arrow['jmp'],
                }
                return res
            # 带有title的上传
            print(cur['words'])
            if "+" in nextOne['words']:
                upload = handleUpload(result,index + 1)
                jmp = 0

                for i in range(len(upload)):
                    item = upload[i]
                    jmp += item['jmp']
                    del item['jmp']

                if len(upload) > 1:
                    return {
                        "type":"group",
                        "key":cur['words'],
                        "children":upload,
                        "jmp":jmp,
                    }
                else:
                    upload[0]['key'] = cur['words']
                    upload[0]['jmp'] = jmp
                    return upload[0]

            raise Exception("next 错误")
        except Exception as e:
            return {
                "type": "title",
                "key": cur['words'],
                "jmp": 0
            }

    return res
#计算子标题
#参数: 第一个子标题所在的位置
def computedSub(result,index,sub_box = []):
    item = result[index]
    sub_box.append(item['words'])
    nextOne = result[index+1]
    # 纵向距离
    colDistance = nextOne['location']['top'] - item["location"]["top"] - item['location']['height']
    if colDistance < TOP_PRECISION:
        return computedSub(result,index + 1,sub_box)
    return sub_box

# 计算横向排版数据
def computedRow(result,index,dis):
    res = {}
    cur = result[index]
    nextOne = result[index + 1]
    nextTwo = {}
    try:
        nextTwo = result[index + 2]
    except:
        nextTwo = {
            'words':""
        }

    if dis > LEFT_PRECISION:
        # 判断是否是选择框
        # 判断方式 , 当前项默认为标题 , 下一项如果包含"请选择" 三个字 , 则是是选择框
        # 进一步 , 判定下一项是否包含 ">" 符号
        # 如果有则去掉该符号 , 跳过当前次数即可
        # 如果没有 , 则判定第二项里是否有 ">" 然后跳过这两项 ( 因为识图有时候会把>和前一项放到一起去)
        if "请选择" in cur['words'] and '>' in nextOne['words']:
            return {
                "type":"select",
                "prop":"",
                "options":[],
                "styleType":"cell",
                "cellLabel":cur['words'],
                "jmp":1,
            }
        if "请选择" in nextOne['words'] :
            arrow = handleSelectArrow(nextOne,nextTwo)
            return {
                "type":"select",
                "prop":"",
                "placeholder":arrow['words'],
                "options":[],
                "styleType":"cell",
                "cellLabel":cur['words'],
                "jmp":arrow["jmp"],
            }
        else:
            return {
                "type": "show",
                "key":cur['words'],
                "val": nextOne['words'],
                "jmp": 1,
            }
    return res

# 处理当为选择框时候 , 右侧箭头
def handleSelectArrow(next_one,next_two):
    jmp = 1
    if ">" in next_one['words'] or not next_two:
        next_one['words'] = next_one['words'].replace(">", "")
        jmp = 1
    elif ">" in next_two['words']:
        jmp = 2
    return {
        "jmp":jmp,
        "words":next_one['words']
    }
#  处理上传组件的信息
# 这里的index是上传组件的index 不是title的index
# 更准确的说是这个+号的index
def handleUpload(result,index):
    item = result[index]
    nextOne = result[index + 1]
    i = index
    while result[i]['words'] == "+":
        i += 1
    # 这里代表了一行有几个+号
    num = i - index
    # 跳过 上传图片 字样
    i += num
    # 判断下一行是否是横向排版 ， 如果是 ， 基本说明是上传组件的底部提示
    # 如果不是 ， 就跳过
    composing = computedComposing(result[i],result[i+1])
    res = []
    # 计算数据
    j = 0
    while j < num :
        res.append({
            "type": "upload",
            "key": result[i+j]['words'] if not composing[2] else None,
            "prop": "prop",
            "maxLength": 1,
            "maxSize": 10240,
            "jmp":3 if not composing[2] else 2,
        })
        j += 1
    if not composing[2]:
        i += num

    try:
        if result[i]['words'] == "+":
            res.extend(handleUpload(result, i))
            pass
    except:
        pass
    return  res

# 计算排版
# return [纵向距离 ， 横向距离 ， 是否是纵向排版]
def computedComposing(current,next_one):
    colDistance = 100
    rowDistance = -100
    if next_one:
        colDistance = next_one['location']['top'] - current["location"]["top"] - current['location']['height']
        rowDistance = next_one['location']['left'] - current["location"]["width"] - current['location']['left']

    # 是否是纵向排版
    isCol = rowDistance < 0
    return [colDistance,rowDistance,isCol]